package com.jspgou.cms.action.admin.main;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.common.web.ResponseUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.cms.entity.Category;
import com.jspgou.cms.entity.Coupon;
import com.jspgou.cms.entity.ProductType;
import com.jspgou.cms.manager.CategoryMng;
import com.jspgou.cms.manager.CouponMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.ProductTypeMng;
import com.jspgou.cms.web.SiteUtils;

@Controller
public class CouponAct {
	private static final Logger log = LoggerFactory.getLogger(CouponAct.class);

	@RequestMapping("/coupon/v_add.do")
	public String add(HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Website site = SiteUtils.getWeb(request);
		String upRoot = site.getUploadUrl();
		List<ProductType> tlist = productTypeMng.getList(site.getId());
		if (tlist.size() > 0) {
			List<Category> caList = categoryMng.getListBypType(site.getId(),
					tlist.get(0).getId(),null);
			model.addAttribute("caList", caList);
		}

		model.addAttribute("pTypeList", productTypeMng.getList(site.getId()));
		model.addAttribute("uploadRoot", upRoot);
		return "coupon/add";
	}

	@RequestMapping("/coupon/o_selectCategory.do")
	public String change(Long id, HttpServletResponse response,
			HttpServletRequest request) throws JSONException {
		Website site = SiteUtils.getWeb(request);
		List<Category> caList = categoryMng.getListBypType(site.getId(), id,null);
		JSONObject jb = new JSONObject();
		for (int i = 0; i < caList.size(); i++) {
			jb.append(i + "", caList.get(i).getName());
		}
		ResponseUtils.renderJson(response, jb.toString());
		return null;
	}

	@RequestMapping("/coupon/o_save.do")
	public String save(Long pTypeid,Long categoryId, Coupon bean, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website site = SiteUtils.getWeb(request);
		if(categoryId!=null)
		bean.setCategory(categoryMng.findById(categoryId));
		manager.save(bean, site.getId(), pTypeid);
		return this.list(request, response, model);
	}

	@RequestMapping("/coupon/v_list.do")
	public String list(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		List<Coupon> cList = manager.getList(null);
		model.addAttribute("list", cList);
		return "coupon/list";
	}

	@RequestMapping("/coupon/v_edit.do")
	public String edit(Long id, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website site = SiteUtils.getWeb(request);
		String upRoot = site.getUploadUrl();
		List<ProductType> ptype=productTypeMng.getList(site.getId());
		model.addAttribute("uploadRoot", upRoot);
		model.addAttribute("coupon", manager.findById(id));
		return "coupon/edit";
	}

	@RequestMapping("/coupon/o_update.do")
	public String update(Long id,Long pTypeid,Long categoryid, Coupon bean, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		if(id!=null)
			bean.setId(id);
		Coupon bean1=manager.update(bean, null);
		return this.list(request, response, model);
	}

	@RequestMapping("/coupon/o_delete.do")
	public String delete(Long[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website site = SiteUtils.getWeb(request);
		String url = site.getUploadUrl();
		 url+=site.getUploadUrl();
		manager.deleteByIds(ids, url);
		return this.list(request, response, model);

	}

	@Autowired
	private CategoryMng categoryMng;
	@Autowired
	private CouponMng manager;
	@Autowired
	private ProductMng productMng;
	@Autowired
	private ProductTypeMng productTypeMng;

}